package com.zjh.photoshopstudy.app.collection;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.zjh.photoshopstudy.Model.VideoEntity;
import com.zjh.photoshopstudy.R;
import com.zjh.photoshopstudy.Utils.Data;
import com.zjh.photoshopstudy.app.history.onItemCheck;
import com.zjh.photoshopstudy.app.video.VideoActivity;

import java.util.List;

public class CollectionAdapter extends RecyclerView.Adapter<CollectionAdapter.CollectionView> {
    private List<VideoEntity> list;
    private Context mContext;
    private boolean isSelect = false;
    private boolean isAllSelect = false;
    private onItemCheck onItemCheck;

    public CollectionAdapter(List<VideoEntity> list, Context mContext, com.zjh.photoshopstudy.app.history.onItemCheck onItemCheck) {
        this.list = list;
        this.mContext = mContext;
        this.onItemCheck = onItemCheck;
    }

    public List<VideoEntity> getList() {
        return list;
    }

    public void setList(List<VideoEntity> list) {
        this.list = list;
    }

    public void setSelect(boolean select) {
        isSelect = select;
    }

    public boolean isSelect() {
        return isSelect;
    }

    public boolean isAllSelect() {
        return isAllSelect;
    }

    public void setAllSelect(boolean allSelect) {
        isAllSelect = allSelect;
    }

    @NonNull
    @Override
    public CollectionView onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new CollectionView(LayoutInflater.from(parent.getContext()).inflate(R.layout.collection_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final CollectionView holder, final int position) {
        final VideoEntity entity = list.get(position);
        Glide.with(mContext).load(Data.webRoot_URL + entity.getvThumbnailPath()).into(holder.mImage);
        Glide.with(mContext).load(Data.webRoot_URL + entity.getvAppUserEntity().getuIcon()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(holder.mUserImage);
        holder.mName.setText(entity.getvAppUserEntity().getuName());
        holder.mTitle.setText(entity.getvTitle());
        if (isSelect) {
            holder.checkBox.setVisibility(View.VISIBLE);
        } else {
            holder.checkBox.setVisibility(View.INVISIBLE);
        }
        if (isAllSelect) {
            holder.checkBox.setChecked(true);
        } else {
            holder.checkBox.setChecked(false);
        }
        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    onItemCheck.onItemCheck(list.get(position));
                } else {
                    onItemCheck.onItemCancelCheck(list.get(position));
                }
            }
        });
        holder.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!isSelect) {
                    Intent intent = new Intent(mContext, VideoActivity.class);
                    intent.putExtra("videoEntity", entity);
                    mContext.startActivity(intent);
                } else {
                    holder.checkBox.setChecked(true);
                }
            }

        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class CollectionView extends RecyclerView.ViewHolder {
        ConstraintLayout layout;
        ImageView mImage;
        ImageView mUserImage;
        TextView mTitle;
        TextView mName;
        CheckBox checkBox;


        public CollectionView(@NonNull View itemView) {
            super(itemView);
            mImage = itemView.findViewById(R.id.imageView4);
            mUserImage = itemView.findViewById(R.id.collection_uicon);
            mTitle = itemView.findViewById(R.id.title2);
            mName = itemView.findViewById(R.id.collection_name);
            layout = itemView.findViewById(R.id.collection_item);
            checkBox = itemView.findViewById(R.id.checkBox2);
        }
    }
}
